//package com.hss.springboot2.controller;
//
//import com.hss.springboot2.entity.UserEntity;
//import com.hss.springboot2.jpa.UserJPA;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.jpa.domain.Specification;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.persistence.criteria.CriteriaBuilder;
//import javax.persistence.criteria.CriteriaQuery;
//import javax.persistence.criteria.Predicate;
//import javax.persistence.criteria.Root;
//import javax.servlet.http.HttpServletRequest;
//import java.util.List;
//import java.util.Optional;
//
//@RestController
//@RequestMapping(value = "/user")
//public class UserController {
//    @Autowired
//    UserJPA userJPA;
//
//    @RequestMapping(value = "/list", method = RequestMethod.GET)
//    public List<UserEntity> listAll(){
//        return userJPA.findAll();
//    }
//
//
//    @RequestMapping(value = "/save", method = RequestMethod.GET)
//    public UserEntity save(UserEntity entity){
//        return userJPA.save(entity);
//    }
//
//    @RequestMapping(value = "/delete", method = RequestMethod.GET)
//    public List<UserEntity> delete(long id){
//         userJPA.deleteById(id);
//        return userJPA.findAll();
//
//    }
//
//    @RequestMapping(value = "/login")
//    public String login(UserEntity  user, HttpServletRequest request){
//        boolean flag = true;
//        String result = "登录成功";
//        Optional<UserEntity> userEntity = userJPA.findOne(new Specification<UserEntity>() {
//            @Override
//            public Predicate toPredicate(Root<UserEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
//                 criteriaQuery.where(criteriaBuilder.equal(root.get("name"),user.getUsername()));
//                return null;
//            }
//        });
//        if (userEntity == null){
//            flag = false;
//            result = "用户不存在";
//        }else if (!userEntity.get().getPassword().equals(user.getPassword())){
//            result = "密码错误";
//            flag = false;
//
//        }
//        if (flag){
//            request.getSession().setAttribute("_session_user",userEntity);
//        }
//        return result;
//    }
//}
